﻿using System;
using System.Collections.Generic;
using System.Text;
using PreeRegisterModel;
using Newtonsoft.Json.Linq;
using System.Web;

namespace PreeRegisterBLL
{
    public class DeptUpdateBLL
    {
        /// <summary>
        /// 科室更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static DeptUpdateResponseBodyModel DeptUpdate(DeptUpdateRequestBodyModel request)
        {
            try
            {
                //获取科室更新信息
                DeptUpdateResponseBodyModel deptUpdateResponseBodyModel = GetDeptUpdate(request);

                return deptUpdateResponseBodyModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }

        /// <summary>
        /// 科室更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        //private static DeptUpdateResponseBodyModel GetDeptUpdate(DeptUpdateRequestBodyModel request)
        //{
        //    try
        //    {
        //        //查询科室表
        //        DeptUpdateResponseBodyModel deptUpdateResponseBodyModel = new PreeRegisterModel.DeptUpdateResponseBodyModel();

        //        DataSet ds = DeptUpdateDAL.GetDeptUpdateInfo(request.id);

        //        //获取科室更新情况
        //        List<DeptUpdateModel> DeptUpdateModel_list = new List<DeptUpdateModel>();

        //        foreach (DataRow drDept in ds.Tables["DictDept"].Rows)
        //        {
        //            DeptUpdateModel deptUpdateModel = new DeptUpdateModel();

        //            deptUpdateModel.id = drDept["No_Dept"].ToString();
        //            deptUpdateModel.name = drDept["DeptName"].ToString();

        //            DeptUpdateModel_list.Add(deptUpdateModel);
        //        }

        //        deptUpdateResponseBodyModel.hospid = VariableBLL.HospitalId;
        //        deptUpdateResponseBodyModel.depts = DeptUpdateModel_list;

        //        //返回
        //        return deptUpdateResponseBodyModel;
        //    }
        //    catch (Exception err)
        //    {

        //        throw;
        //    }
        //}


        /// <summary>
        /// 获取科室信息
        /// </summary>
        /// <returns></returns>
        public static DeptUpdateResponseBodyModel GetDeptUpdate(DeptUpdateRequestBodyModel request)
        {
            try
            {
                string url = CommonBLL.GetConfig("CZHP_ADDR") + "GetOutpatientDepartments?parameters=";
                string postData = "{head:\"\",body:\"\"}";
                url += postData;
                CommonBLL.WriteFile_New(CommonBLL.bizCode_deptUpdate, "2：获取科室信息接口请求密文：\r\n" + url);
                JObject obj = CommonBLL.HttpPost_CZHP(HttpUtility.UrlEncode(url));
                CommonBLL.WriteFile_New(CommonBLL.bizCode_deptUpdate, "3：获取科室信息接口返回明文：\r\n" + CommonBLL.ConvertJsonString(obj.ToString()));
                DeptUpdateResponseBodyModel deptInfoRequestModel = new DeptUpdateResponseBodyModel();
                deptInfoRequestModel.hospid = VariableBLL.HospitalId;
                List<DeptUpdateModel> depts = new List<DeptUpdateModel>();
                string code = obj["code"].ToString();
                if (string.Equals(code, "1"))
                {
                    var mJObj = JArray.Parse(obj["result"].ToString());
                    foreach (var dr in mJObj)
                    {
                        //查询所有科室信息，通过ID匹配返回请求的科室信息
                        if (string.Equals(request.id, dr["DepartmentCode"].ToString()))
                        {
                            DeptUpdateModel dept = new DeptUpdateModel();
                            dept.id = dr["DepartmentCode"].ToString();
                            dept.name = dr["Name"].ToString();
                            dept.tel = "";
                            depts.Add(dept);
                        }
                    }
                    deptInfoRequestModel.depts = depts;
                }
                return deptInfoRequestModel;
            }
            catch (Exception err)
            {
                CommonBLL.WriteFile_New(CommonBLL.bizCode_searchNopay, err.ToString());
                throw;
            }
        }

        /// <summary>
        /// 获取科室信息
        /// </summary>
        /// <returns></returns>
        public static DeptUpdateResponseBodyModel GetDeptInfo(string deptId, bool isGetDoctor, out DoctorUpdateResponseBodyModel doctorInfoRequestModel)
        {
            try
            {
                doctorInfoRequestModel = new DoctorUpdateResponseBodyModel();
                string url = DrSoftsFrame.BLL.CommonBLL.InCommon.ReadConfig("CZHP_ADDR", "") + "GetOutpatientDepartments?parameters=";
                string postData = "{head:\"\",body:\"\"}";
                url += postData;
                CommonBLL.WriteFile_New(CommonBLL.bizCode_deptUpdate, "2：获取科室信息接口请求密文：\r\n" + url);
                JObject obj = CommonBLL.HttpPost_CZHP(url);
                CommonBLL.WriteFile_New(CommonBLL.bizCode_deptUpdate, "3：获取科室信息接口返回明文：\r\n" + CommonBLL.ConvertJsonString(obj.ToString()));
                DeptUpdateResponseBodyModel deptInfoRequestModel = new DeptUpdateResponseBodyModel();
                //deptInfoRequestModel.hospid = VariableBLL.hospid;
                //deptInfoRequestModel.opcode = "10";
                List<DeptUpdateModel> depts = new List<DeptUpdateModel>();
                string code = obj["code"].ToString();
                if (string.Equals(code, "1"))
                {
                    var mJObj = JArray.Parse(obj["result"].ToString());
                    foreach (var dr in mJObj)
                    {
                        if (string.Equals(deptId, dr["DepartmentCode"].ToString()))
                        {
                            DeptUpdateModel dept = new DeptUpdateModel();
                            dept.id = dr["DepartmentCode"].ToString();
                            dept.name = dr["Name"].ToString();
                            dept.tel = "";

                            depts.Add(dept);
                        }
                    }
                    deptInfoRequestModel.depts = depts;
                }
                if (isGetDoctor)
                {
                    List<DoctorUpdateModel> doctors = new List<DoctorUpdateModel>();
                    foreach (DeptUpdateModel dept in deptInfoRequestModel.depts)
                    {
                        string url_doctor = DrSoftsFrame.BLL.CommonBLL.InCommon.ReadConfig("CZHP_ADDR", "") + "GetOutpatientDoctors?parameters=";
                        StringBuilder head = new StringBuilder();
                        StringBuilder body = new StringBuilder();
                        head.Append("{head:\"\"");
                        body.Append("{");
                        body.AppendFormat("\"DepartmentCode\":\"{0}\",", dept.id.ToString());
                        body.Append("\"Date\":\"\"");
                        body.Append("}");

                        string postData_doctor = string.Format("{0},body:\"{1}", head.ToString(), HttpUtility.UrlEncode(Security.Base64.EncodeBase64(body.ToString())));
                        postData_doctor += "\"}";
                        url_doctor += postData_doctor;
                        JObject obj_doctor = CommonBLL.HttpPost_CZHP(url_doctor);
                        string code_doctor = obj_doctor["code"].ToString();
                        if (string.Equals(code_doctor, "1"))
                        {
                            var mJObj_doctor = JArray.Parse(obj_doctor["result"].ToString());
                            foreach (var dr in mJObj_doctor)
                            {
                                DoctorUpdateModel doctor = new DoctorUpdateModel();
                                doctor.id = dr["DoctorCode"].ToString();
                                doctor.name = dr["Name"].ToString();
                                doctor.did = dept.id;
                                doctor.dname = dept.name;
                                doctor.gender = "";
                                doctor.rank = "";
                                doctor.randid = "";
                                doctor.wrank = "";
                                doctor.info = "";
                                doctor.kword = "";
                                doctor.picurl = "";

                                doctors.Add(doctor);
                            }
                            doctorInfoRequestModel.doctors = doctors;
                        }
                    }
                }
                return deptInfoRequestModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }
    }
}
